﻿using CC.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace CCWpf.ViewModels
{
    public class LoginViewModel:ViewModelBase
    {
        public LoginViewModel()
        {
            SelectRole(2);
            InitCommands();
        }
        private void InitCommands()
        {
            CheckIsLogin = new RelayCommand(o => 
            {
                CheckLogin();
            });
            SelectRoleCmd = new RelayCommand(code =>
            {
                int roleCode = Convert.ToInt32(code);
                SelectRole(roleCode);
            });

            StuRegisterCmd = new RelayCommand(o =>
            {
               ShowStuRegisterWindow();
            });

            LoginCmd = new RelayCommand(win =>
            {
                LoginSystem(win);
            });
        }
        private void LoginSystem(object win)
        {
            string enPwd = UserPwd;
            //调用登录 核对账号密码

            LoginInfo loginInfo = new LoginInfo
            {
                UserState = true,
                UserName = "张三",
                LoginId = 10,
                LoginName = "zhangsan",
                RoleCode=2,

            };//登录者信息

            if (loginInfo != null) { 
            
                UserId = loginInfo.LoginId;
                LoginUser = loginInfo;
                if (!loginInfo.UserState)
                { 
                    ShowError("登录失败", "该账号已冻结不能登录");
                }
                else {
                    //修改登录状态
                    //userDll.update....
                    //关闭登录页
                    CloseWindow("loginWin", win);
                }
            }

        }
        /// <summary>
        /// 显示学生注册页
        /// </summary>
        private void ShowStuRegisterWindow()
        {
            StudentRegisterViewModel registerVM = new StudentRegisterViewModel();
            ShowDialog("registerWindow", registerVM);
            if (registerVM.IsRegisterSucess)
            { 
                UserName = registerVM.StudentNo;
            }
        }

        private void SelectRole(int roleCode)
        {
            SelectRoleCode = roleCode;//设置角色代号
            switch (roleCode)
            {
                case 1:
                case 2:
                    IsAdmin = true;
                    IsTeacher = false;
                    IsStudent = false;
                    ShowRegisterBtn = Visibility.Collapsed;
                    break;
                case 3:
                    IsAdmin = false;
                    IsTeacher = true;
                    IsStudent = false;
                    ShowRegisterBtn = Visibility.Collapsed;
                    break;
                case 4:
                    IsAdmin = false;
                    IsTeacher = false;
                    IsStudent = true;
                    ShowRegisterBtn = Visibility.Visible;
                    break;
            }
        }

        private void CheckLogin()
        {
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(UserPwd))
            {
                IsLogin = false;
            }
            else if (SelectRoleCode > 4 || SelectRoleCode <= 0)
            {
                IsLogin = false;
            }
            else
            {
                IsLogin = true;
            }
        }


        #region 属性
        public int UserId { get; set; }

        public bool IsLogin_Success { get { return UserId > 0; } }

        public LoginInfo LoginUser { get; set; }=new LoginInfo();

        private string userName;
        /// <summary>
        ///登录账户
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { userName = value; OnPropertyChanged(); }
        }


        private string userPwd;
        /// <summary>
        /// 登录密码
        /// </summary>
        public string UserPwd
        {
            get { return userPwd; }
            set { userPwd = value; OnPropertyChanged(); }
        }
         
        /// <summary>
        ///用户角色代码 2教务 3教师 4学生
        /// </summary>
        public int SelectRoleCode { get; set; }


        private bool isAdmin = true;
        /// <summary>
        /// 是否教务
        /// </summary>
        public bool IsAdmin
        {
            get { return isAdmin; }
            set { isAdmin = value; OnPropertyChanged(); }
        }

        private bool isTeacher = false;
        /// <summary>
        /// 是否教师
        /// </summary>
        public bool IsTeacher
        {
            get { return isTeacher; }
            set { isTeacher = value; OnPropertyChanged(); }
        }

        private bool isStudent = false;
        /// <summary>
        /// 是否学生
        /// </summary>
        public bool IsStudent
        {
            get { return isStudent; }
            set { isStudent = value; OnPropertyChanged(); }
        }
        private bool isLogin = false;
        /// <summary>
        /// 是否可以登录
        /// </summary>
        public bool IsLogin
        {
            get { return isLogin; }
            set { isLogin = value; OnPropertyChanged(); }
        }

        public Visibility showRegisterBtn = Visibility.Collapsed;
        /// <summary>
        /// 是否显示注册按钮
        /// </summary>
        public Visibility ShowRegisterBtn
        {
            get { return showRegisterBtn; }
            set { showRegisterBtn = value; OnPropertyChanged(); }
        }
        #endregion

        #region 命令
        //登录命令
        public ICommand LoginCmd { get; set; }
        //
        public ICommand CheckIsLogin { get; set; }
        //选择身份
        public ICommand SelectRoleCmd { get; set; }
        //用户注册
        public ICommand StuRegisterCmd { get; set; }
        
        #endregion
    }
}
